home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #27 (Dec 87) / c daisy printer driver / PACK.c < prev    next >
Text File  |  1987-09-29  |  6KB  |  188 lines

  1. /*
  2.  * Code for PACK ID -4096 to be used with the Chooser to set the
  3.  * printing port options.  Set the Flags longword in the PACK
  4.  * resource to 0400E000 after building the PACK resource to make
  5.  * sure we get the right-hand button message.
  6.  */
  7.  
  8. #include <WindowMgr.h>    /* includes QuickDraw.h, MacTypes.h */
  9. #include <DialogMgr.h>
  10. #include "prglobals.h"
  11. /* 
  12.  * Resources which are standard type and accessed by the PACK -4096 resource
  13.  * have ID # RES1ID.  Resources which are non-standard type and/or belong
  14.  * without a doubt to our driver code are accessed with RES2ID.  These include
  15.  * the Stng resource and our PREC #-8192.  (IM 4 says standard resource types
  16.  * to be used by this PACK resource should have IDs in the range -4080 to
  17.  * -4065.)
  18.  */
  19.  
  20. /* Printer Setup Dbox item numbers… */
  21. #define SAVEITEM     1
  22. #define MODEM        5
  23. #define PRINTER        6
  24. #define BAUDBUTTON    8
  25. #define EOLITEM        11
  26. #define    INITITEM    12
  27. #define    TOPITEM        13
  28. #define    EOPITEM        14
  29. #define    EOFITEM        15
  30. #define CTSITEM        19
  31. #define XONXOFFITEM    20
  32. #define CANCELITEM    21
  33. #define NUMSTRINGS    5
  34. #define RESPAD        24
  35.  
  36. pascal OSErr main(message,caller,objname,zonename,p1,p2)
  37. int    message,caller;
  38. StringPtr    objname,zonename;
  39. long    p1,p2;
  40. {
  41.     if (message == buttonMsg){
  42.             prsetup();
  43.     }
  44.     return (noErr);
  45. }
  46. pushradiobutton(thedialog,itemhit,first,last)    /* push a radio Button */
  47. DialogPtr thedialog;                /* set itemhit, unset  */
  48. int itemhit,first,last;                /* all others in range */
  49. {
  50.       int itemtype,i;
  51.       Handle itemhandle;            /* Does check boxes, too. */
  52.       Rect itemrect;                /* (when range is 1 in size.) */
  53.     if(first ==0) return;
  54.     for(i=first-1;last-i++;){
  55.         GetDItem(thedialog,i,&itemtype,&itemhandle,&itemrect);
  56.         if(i == itemhit) SetCtlValue(itemhandle,1);
  57.         else SetCtlValue(itemhandle,0);
  58.     }
  59. }
  60. prsetup()
  61. {
  62. DialogPtr printdialog;
  63. WindowPtr tempport;
  64. int     itemhit,i,baudtype,edittype,donetype;
  65. Handle    bauditem,doneitem,edititem;
  66. Rect    baudbox,donebox,editbox;
  67. Str255    thestring;
  68. unsigned char    *strptr;
  69. long    length,result;
  70. BAUDS    **mybauds;
  71. Pfg    settings;
  72. StrList    mystrings;
  73.     mybauds = (BAUDS **)GetResource('PREC',RES2ID);
  74.     if(mybauds == nil) return;
  75.     if ((settings = (Pfg)(GetResource('Stng',RES2ID))) == nil ||
  76.             (mystrings = (StrList)
  77.                 (GetResource('STR#',RES1ID))) == nil)
  78.         return;
  79.     if (pbaud<0 || pbaud>9) pbaud = 0;
  80.     if((printdialog = GetNewDialog(RES1ID, 0L,(WindowPtr) -1)) == nil)
  81.         return;
  82.     GetDItem(printdialog,BAUDBUTTON,&baudtype,&bauditem,&baudbox);
  83.     GetDItem(printdialog,SAVEITEM,&donetype,&doneitem,&donebox);
  84.     SetCTitle(bauditem,((*mybauds)+pbaud)->label);
  85. /* This gets the printer control strings from a string list,
  86.  * then sets the editText items in the dialog box to contain
  87.  * the strings.
  88.  */
  89.     strptr = &((*mystrings)->thestrings[0]);
  90.     for(i = EOLITEM-1;EOFITEM - i++;){
  91.         GetDItem(printdialog,i,&edittype,&edititem,&editbox);
  92.         SetIText(edititem,strptr);
  93.         strptr += (*strptr) + 1;
  94.     }
  95.     GetPort(&tempport);
  96.     SetPort(printdialog);
  97.     ShowWindow(printdialog);
  98.     PenSize(4,4);            /* Time to frame some buttons. */
  99.     InsetRect(&donebox,-5,-5);
  100.     FrameRoundRect(&donebox,16,16);
  101.     PenSize(2,2);
  102.     InsetRect(&baudbox,-3,-3);
  103.     FrameRoundRect(&baudbox,12,12);
  104.     pushradiobutton(printdialog, pport + MODEM,MODEM,PRINTER);
  105.     pushradiobutton(printdialog,CTSITEM + XonXoff,CTSITEM,
  106.         XONXOFFITEM);
  107.     itemhit = 0;
  108.         while(itemhit !=1){
  109.               ModalDialog(0L,&itemhit);
  110.               switch(itemhit){
  111.                     /* Port change.  It might be nice to check and see
  112.                      * whether AppleTalk is active if the user selects
  113.                      * the Printer Port… */
  114.                     case MODEM:        
  115.                     case PRINTER:
  116.                         pport = itemhit - MODEM;
  117.                 pushradiobutton(printdialog,itemhit,
  118.                    MODEM,PRINTER);
  119.                         break;
  120.                     case BAUDBUTTON:    /* next baud rate change */
  121.                         /* Ten radio buttons would be just too much. */
  122.                         if(++pbaud == 10) pbaud = 0;
  123.                  SetCTitle(bauditem,((*mybauds)+pbaud)->label);
  124.                  break;
  125.                     case CTSITEM:
  126.                     case XONXOFFITEM:
  127.                          XonXoff = itemhit - CTSITEM;
  128.                 pushradiobutton(printdialog,
  129.                     itemhit,CTSITEM,XONXOFFITEM);
  130.                         break;
  131.                     case CANCELITEM:
  132.                     DisposDialog(printdialog);
  133.                     SetPort(tempport);                          
  134.                         return;
  135.                         break;
  136.             }
  137.     }
  138.     /* The user has set the baud rate and the port, and also possibly
  139.      * edited the printer control strings.  Since we used ModalDialog()
  140.      * with no filterproc we don't know whether any of the strings have
  141.      * been changed.  Therefore we just rebuild the whole string list.
  142.      * First, determine the length.
  143.      */
  144.     length = (long) (sizeof(int)+RESPAD);
  145.     for(i = EOLITEM-1;EOFITEM - i++;){
  146.         GetDItem(printdialog,i,&edittype,&edititem,&editbox);
  147.         GetIText(edititem,thestring);
  148.         length += (long) thestring[0];
  149.     }
  150.     /* Size might have changed, so we unlock the handle and attempt to
  151.      * resize it. */
  152.     asm{
  153.         move.l    mystrings,a0    ;;  This is to save loading MacTraps
  154.         _HUnlock
  155.         move.l    mystrings,a0
  156.         move.l    length,d0
  157.         _SetHandleSize
  158.         move.l    mystrings,a0
  159.         _GetHandleSize
  160.         move.l    d0,result
  161.     }
  162.     if ( result != length ){    /* Abort on error. */
  163.         DisposDialog(printdialog);
  164.         SetPort(tempport);                          
  165.         return(FALSE);
  166.     }
  167.     asm{
  168.         move.l    mystrings,a0
  169.         _HNoPurge
  170.         move.l    mystrings,a0
  171.         _HLock
  172.     }
  173.     /* Rebuild the STR# from the item list. */
  174.     strptr = &((*mystrings)->thestrings[0]);
  175.     for(i = EOLITEM-1;EOFITEM - i++;){
  176.         GetDItem(printdialog,i,&edittype,&edititem,&editbox);
  177.         GetIText(edititem,strptr);
  178.         strptr += (*strptr) + 1;
  179.     }
  180.     DisposDialog(printdialog);
  181.     SetPort(tempport); 
  182.     ChangedResource(settings);
  183.     ChangedResource(mystrings);                        
  184.     WriteResource(settings);
  185.     WriteResource(mystrings);
  186.     return;
  187. }
  188.